#!/usr/bin/env python3
from scapy.all import Ether, IP, IPv6, ICMP, sendp, get_if_hwaddr
from scapy.packet import Packet, bind_layers
from scapy.fields import ByteField

# 自定义 EtherType
SR_TYPE = 0x0908

# 自定义 Source Routing header
class SourceRouting(Packet):
    name = "SourceRouting"
    fields_desc = [
        ByteField("path_len", 3),
        ByteField("current_index", 0),
        ByteField("port0", 4),  # ✅ 替换为你自己拓扑的路径端口
        ByteField("port1", 2),
        ByteField("port2", 1),
        ByteField("port3", 0)
    ]

# 绑定 SR header
bind_layers(Ether, SourceRouting, type=SR_TYPE)

def main():
    iface = "eth0"
    src_mac = get_if_hwaddr(iface)
    dst_mac = "08:00:00:00:01:00"  # s1 入口 MAC（或下一跳）

    # IPv4 ping packet
    pkt_ipv4 = Ether(src=src_mac, dst=dst_mac, type=0x0800) / IP(src="10.0.1.1", dst="10.0.2.2") / ICMP()

    # IPv6 ping packet
    pkt_ipv6 = Ether(src=src_mac, dst=dst_mac, type=0x86DD) / IPv6(src="fe80::1234", dst="fe80::5678") / ICMP()

    # SR ping packet
    sr_hdr = SourceRouting()
    pkt_sr = Ether(src=src_mac, dst=dst_mac, type=SR_TYPE) / sr_hdr / IP(src="10.0.1.1", dst="10.0.2.2") / ICMP()

    print("🚀 Sending multi-modal packets (IPv4 + IPv6 + SR)...")
    sendp([pkt_ipv4, pkt_ipv6, pkt_sr], iface=iface, verbose=False)

if __name__ == "__main__":
    main()
